package bd;
import java.util.*;
import java.sql.*;
import classes.*;
import javax.swing.JOptionPane;

public class LocalBD {

    public static boolean salvar(Local local, Usuario usuarioLogado){
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_LOCAL(NULL,?,?,?,?,NULL,?,?)}");
            //procedure.setInt(1, (Integer)null);
            procedure.setString(1, local.getNome());
            procedure.setInt(2, local.getNumero());
            procedure.setInt(3,local.getCep());
            procedure.setInt(4,local.getEndereco().getId());
            //0procedure.setInt(6, (Integer)null);
            procedure.setInt(5, usuarioLogado.getId());
            procedure.setString(6, "IN");
            procedure.execute();
            procedure.close();
            conexao.close();

            return true;
        }catch(Exception excecao){
            JOptionPane.showMessageDialog(null,"Não foi possível deletar.");
            return false;
        }
    }

    public static boolean alterar(Local local, Usuario usuarioLogado){
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_LOCAL(?,?,?,?,?,?,?,?)}");
            procedure.setInt(1, local.getId());
            procedure.setString(2, local.getNome());
            procedure.setInt(3, local.getNumero());
            procedure.setInt(4,local.getCep());
            procedure.setInt(5,local.getEndereco().getId());
            procedure.setInt(6, usuarioLogado.getId());
            procedure.setInt(7,(Integer)null);
            procedure.setString(8, "AL");
            procedure.execute();
            procedure.close();
            conexao.close();

            return true;
        }catch(Exception excecao){
            JOptionPane.showMessageDialog(null,"Não foi possível deletar.");
            return false;
        }
    }

    public static boolean deletar(int codigo){
        try{
            Connection conexao = Conexao.abrir();
            CallableStatement procedure = conexao.prepareCall("{call AT_LOCAL(?,?,?,?,?,?,?,?)}");
            procedure.setInt(1, codigo);
            procedure.setString(2, null);
            procedure.setInt(3, (Integer)null);
            procedure.setInt(4,(Integer)null);
            procedure.setInt(5,(Integer)null);
            procedure.setInt(6, (Integer)null);
            procedure.setInt(7, (Integer)null);
            procedure.setString(8, "");
            procedure.execute();
            procedure.close();
            conexao.close();

            return true;
        }catch(Exception excecao){
            JOptionPane.showMessageDialog(null, codigo);
            return false;
        }
    }

    public static Local selecionar(int codigo){
        Local retorno = null;
        try{
            Connection conexao = Conexao.abrir();
            Statement comandoSelect = conexao.createStatement();
            ResultSet resultado = comandoSelect.executeQuery("select * from VIEW_SELECIONALOCAL where loc_id="+codigo+"");
            if(resultado.next())
            {
                Local novoLocal = new Local();
                novoLocal.setId(codigo);
                novoLocal.setNome(resultado.getString("loc_nome"));
                novoLocal.setNumero(resultado.getInt("loc_numero"));
                novoLocal.setCep(resultado.getInt("loc_cep"));
                novoLocal.setEndereco(EnderecoBD.selecionar(resultado.getInt("end_id")));

                retorno=novoLocal;
            }
            conexao.close();
            return retorno;
        }catch(Exception excecao){
            return null;
        }
    }

    public static ArrayList<Local> selecionar(){
        ArrayList<Local> vetLocal = new ArrayList<Local>();
        try{
            Connection conexao = Conexao.abrir();
            Statement comandoSelect = conexao.createStatement();
            ResultSet resultado = comandoSelect.executeQuery("select * from VIEW_SELECIONALOCAL");

            while(resultado.next())
            {
                Local novoLocal = new Local();
                novoLocal.setId(resultado.getInt("loc_id"));
                novoLocal.setNome(resultado.getString("loc_nome"));
                novoLocal.setNumero(resultado.getInt("loc_numero"));
                novoLocal.setCep(resultado.getInt("loc_cep"));
                novoLocal.setEndereco(EnderecoBD.selecionar(resultado.getInt("end_id")));

                vetLocal.add(novoLocal);
            }

            conexao.close();

        }catch(Exception excecao){

        }
        return vetLocal;
    }

}

